﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using com.dz.entity;
using com.dz.dataaccess;
using com.dz.common;
using com.dz.httpmoudle;

namespace com.dz.components
{
    public class UserManager
    {
        UserDao m_user_dao = new UserDao();
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="i_script"></param>
        /// <returns></returns>
        public string RegisterUser(string i_script)
        {
            if (string.IsNullOrEmpty(i_script))
                return "参数错误";

            string[] t_arr = i_script.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);

            if (t_arr.Length != 5)
                return "参数错误";


            return RegisterUser(t_arr[1],t_arr[2],t_arr[0],t_arr[4],t_arr[3]);

        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_user_password"></param>
        /// <param name="i_user_email"></param>
        /// <returns></returns>
        public string RegisterUser(string i_user_name, string i_user_password, string i_user_email,string i_verify_code,string i_nick_name)
        {
            string cookieVfCode = CookieManager.GetVifCode();
            //验证验证码
            string[] cookieVfArr = cookieVfCode.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            if (cookieVfArr == null || cookieVfArr.Length != 3)
                return "验证码格式信息有误";

            //验证时间
            string vfCodeDt = cookieVfArr[0].ToString();
            DateTime dt = DateTime.Now;
            if (!DateTime.TryParse(vfCodeDt, out dt))
                dt = DateTime.MinValue;

            TimeSpan timeSpan = DateTime.Now.Subtract(dt);
            if (timeSpan.Seconds > 60)
                return "验证码已过期";

            //验证验证码信息
            if (string.Compare(i_verify_code, cookieVfArr[1].ToString(), true) != 0)
                return "验证码错误";

            if (string.Compare(SystemUtil.MD5Mac(i_verify_code + Consts.VifCode), cookieVfArr[2].ToString(), true) != 0)
                return "验证码信息错误";

            long t_user_id = 0;

   

            try
            {
                t_user_id = m_user_dao.RegisterUser(i_user_name, i_user_password, i_user_email, i_nick_name);

                if (t_user_id == -1)
                    return "用户名已经存在!";

                if (t_user_id > 0)
                {
                    UserToken token = new UserToken(t_user_id, i_user_name, i_nick_name);
                    UserLoginHelper.SetUserTicket(t_user_id.ToString(), token.ToString());
                    return "注册成功";
                }

                return "注册失败";
            }
            catch (Exception ex)
            {
                LogHelper.Error("注册用户失败" + ex.ToString());
                return "注册出现异常";
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_pwd"></param>
        /// <returns></returns>
        public string UserLogin(string i_script)
        {
            try
            {
                if (string.IsNullOrEmpty(i_script))
                    return "登录失败$信息不全";
                string[] arr = i_script.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
                if (arr == null || arr.Length != 2)
                    return "登录失败$信息不全";
                string i_user_name = SystemUtil.MSGFilterImport(arr[0].ToString());
                string i_pwd = SystemUtil.MSGFilterImport(arr[1].ToString());

                DataSet ds = LoginUser(i_user_name, i_pwd);

                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                    return "登录失败$登录失败,请检查登录信息!";
                CookieManager.SetLoginUserInfo(string.Format("{0}${1}", HttpUtility.UrlEncode(ds.Tables[0].Rows[0]["nickName"].ToString()), ds.Tables[0].Rows[0]["userid"]));
                long t_user_id = 0;
                if (!long.TryParse(ds.Tables[0].Rows[0]["userid"].ToString(), out t_user_id))
                    t_user_id = 0;
                UserToken token = new UserToken(t_user_id, ds.Tables[0].Rows[0]["userName"].ToString(), ds.Tables[0].Rows[0]["nickName"].ToString());
                UserLoginHelper.SetUserTicket(ds.Tables[0].Rows[0]["userid"].ToString(), token.ToString());
                return string.Format("{0}${1}", "登录成功", ds.Tables[0].Rows[0]["nickName"]);
            }
            catch (Exception ex)
            {
                LogHelper.Error("登录出现异常" + ex.ToString());
                return "登录失败$程序出现异常,请稍后重试!";
            }
        }

          /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_user_password"></param>
        /// <returns></returns>
        public DataSet LoginUser(string i_user_name, string i_user_password)
        {

            if (string.IsNullOrEmpty(i_user_name) || string.IsNullOrEmpty(i_user_password))
                return null;

            try
            {
                return m_user_dao.LoginUser(i_user_name, i_user_password);
            }
            catch (Exception ex)
            {
                LogHelper.Error("用户登陆失败" + ex.ToString());
                return null;
            }
        }


           /// <summary>
        /// 获取用户评论
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_page_index"></param>
        /// <param name="i_page_size"></param>
        /// <param name="o_total_count"></param>
        /// <returns></returns>
        public DataSet GetUserReview(long i_user_id, int i_page_index, int i_page_size, out int o_total_count)
        {
            o_total_count = 0;
            try
            {
                return m_user_dao.GetUserReview(i_user_id, i_page_index, i_page_size, out o_total_count);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取用户评论数据失败" + ex.ToString());
                return null;
            }
        }

         /// <summary>
        /// 删除用户评论
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_review_id"></param>
        /// <returns></returns>
        public int DeleteUserReview(long i_user_id, long i_review_id)
        {
            try
            {
                return m_user_dao.DeleteUserReview(i_user_id, i_review_id);
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除用户评论失败" + ex.ToString());
                return Consts.Failed;
            }
        }

                /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_email"></param>
        /// <param name="i_password"></param>
        /// <returns></returns>
        public int UpdateUser(long i_user_id, string i_email, string i_password)
        {
            try
            {
                return m_user_dao.UpdateUser(i_user_id, i_email, i_password);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新用户资料失败" + ex.ToString());
                return Consts.Failed;
            }
        }

          /// <summary>
        /// 添加联系我们信息
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_contact_type"></param>
        /// <param name="i_content"></param>
        /// <param name="i_telephone"></param>
        /// <param name="i_qq"></param>
        /// <param name="i_email"></param>
        /// <returns></returns>
        public int AddContactUS(long i_user_id, int i_contact_type, string i_content, string i_telephone, string i_qq, string i_email)
        {
            try
            {
                return m_user_dao.AddContactUS(i_user_id, i_contact_type, i_content, i_telephone, i_qq, i_email);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加联系我们信息失败" + ex.ToString());
                return Consts.Failed;
            }
        }
    }
}
